<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Gass stu model Class
 *
 * 该类作为学生操作访问控制类的数据操作类
 * @author Jason Chan
 */  
class Gass_stu_model extends CI_Model {
    
	// The following method prevents an error occurring when $this->data is modified.
	// Error Message: 'Indirect modification of overloaded property Demo_cart_admin_model::$data has no effect'.
	public function &__get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}
	/**
	 * set_reg_type
	 * 设置学生报考的类型
	 * 参数stuType：2为意向生、1为推免生
	 */
	function set_reg_type($stuType)
	{
		//更新学生信息表中的类型状态
    $update_data = array(
        'upro_reg_type' => $stuType
    );
    $this->db->where('upro_id', $this->flexi_auth->get_user_id());
    $this->db->update('ga_user_profiles', $update_data); 	    	

		// Save any public status or error messages (Whilst suppressing any admin messages) to CI's flash session data.
		$this->session->set_flashdata('message', $this->flexi_auth->get_messages());
		//根据不同的类型，直接去往不同的维护页面
		switch($stuType){
    case 1:
      redirect('gass_auth_students/reg_apply');
      break;  
    case 2:
      redirect('gass_auth_students/int_apply');
      break;
		}			
	}
	/**
	 * 获取招生宣传会列表
	 * 
	 * 必须是尚未过期的
	 */	
	function get_recruit_list()
	{
		return $this->db->query("select * from ga_recruit_activities where activity_over > '".date("Y-m-d",time())."' and activity_begin < '".date("Y-m-d",time())."'  ")->result_array();
		 //$this->db->get_where('ga_recruit_activities',array('activity_over >'=>date("Y-m-d",time())),'activity_begin <'=>date("Y-m-d",time())))->result_array();
	}
	
	/**
	 * get_colleges_list
	 * 获取可以报考的学院LIST
	 */
	function get_colleges_list(){
		return $this->db->query('select distinct college_name,college_id from ga_college_major_tutor')->result_array();
	}
	/**
	 * 获取毕业单位码的LIST
	 * 
	 */	
	function get_bydwm_list()
	{
		return $this->db->get('ga_graduate_colleges')->result_array();
	}
	/**
	 * get_major_list
	 * 获取可以报考的专业LIST
	 */	
	function get_major_list($cId){
    $majorArray = $this->db->query("select * from ga_college_major_tutor where college_id=".$cId." and major_id like '%00' ")->result_array();;
    echo json_encode($majorArray);
	}
	/**
	 * get_tutor_list
	 * 获取可以报考的导师LIST
	 */	
	function get_tutor_list($cId,$mId){
    //需要考虑导师跨专业的问题
    $tutorArray = $this->db->query("select * from ga_college_major_tutor where  major_id like '".substr($mId,0,4)."%' ")->result_array();;
    echo json_encode($tutorArray);
	}
	/**
	 * get_apply_form
	 * 获取已保存的申请数据
	 */	
	function get_apply_form(){
		$this->data['applyInfo'] = $this->db->get_where('ga_apply_info',array('stu_id' => $this->flexi_auth->get_user_id() ))->row_array();
	}
	
	/**
	 * save_apply_form
	 * 保存提交的申请数据
	 */	
	function save_apply_form()
	{
		$this->load->library('form_validation');
		
		$validation_rules = array(
			array('field' => 'update_xxbsm', 'label' => '信息标识码', 'rules' => 'required|max_length[20]'),
			array('field' => 'update_true_name', 'label' => '真实姓名', 'rules' => 'required|max_length[20]'),
			array('field' => 'update_id_num', 'label' => '证件号码', 'rules' => 'required|max_length[50]'),
			array('field' => 'update_undergraduate_major', 'label' => '本科毕业专业', 'rules' => 'required|max_length[50]'),
			array('field' => 'update_undergraduate_id', 'label' => '本科学号', 'rules' => 'required|max_length[20]'),
			array('field' => 'update_undergraduate_date', 'label' => '本科毕业年份', 'rules' => 'required|exact_length[10]'),
			array('field' => 'update_avg_score', 'label' => '平均成绩', 'rules' => 'required|numeric'),
			array('field' => 'update_level_4_score', 'label' => '英语四级成绩', 'rules' => 'required|numeric'),
			array('field' => 'update_level_6_score', 'label' => '英语六级成绩', 'rules' => 'required|numeric'),
			array('field' => 'update_class_ranking', 'label' => '班级排名', 'rules' => 'required|integer'),
			array('field' => 'update_grade_ranking', 'label' => '专业排名', 'rules' => 'required|integer'),
			array('field' => 'update_telephone', 'label' => '联系电话', 'rules' => 'required|max_length[20]'),
			array('field' => 'update_cellphone', 'label' => '移动电话', 'rules' => 'required|max_length[20]'),
			array('field' => 'update_tutor', 'label' => '导师姓名', 'rules' => 'required|max_length[20]'),
			array('field' => 'update_address', 'label' => '通信地址及邮编', 'rules' => 'required|max_length[100]'),
		);
		if($this->input->post('select_apptype')==3)
	  {
	  	$validation_rules[] = array('field' => 'update_examinee_num', 'label' => '考生编号', 'rules' => 'required|max_length[20]');
	  	$validation_rules[] = array('field' => 'update_total_points', 'label' => '初始总分', 'rules' => 'required|numeric');
	  	$validation_rules[] = array('field' => 'update_political_points', 'label' => '政治分数', 'rules' => 'required|numeric');
	  	$validation_rules[] = array('field' => 'update_foreign_language', 'label' => '外国语分数', 'rules' => 'required|numeric');
	  	$validation_rules[] = array('field' => 'update_business_one', 'label' => '业务课一分数', 'rules' => 'required|numeric');
	  	$validation_rules[] = array('field' => 'update_business_two', 'label' => '业务课二分数', 'rules' => 'required|numeric');
	  	$validation_rules[] = array('field' => 'update_initial_major', 'label' => '一志愿报考专业代码', 'rules' => 'required|max_length[15]');
	  }
		$this->form_validation->set_rules($validation_rules);
		if ($this->form_validation->run()){
			$undergraduate_date = date_create_from_format('m/d/Y',$this->input->post('update_undergraduate_date'));	
			$profile_data = array(
				'stu_id' => $this->flexi_auth->get_user_id(),
				'apply_type' => $this->input->post('select_apptype'),
				'identifying_code' => $this->input->post('update_xxbsm'),
				'statue_reg_num' => $this->input->post('update_bmh'),
				'stu_real_name' => $this->input->post('update_true_name'),
				'recom_qualify' => $this->input->post('select_recom_qualify'),
				'stu_gender' => $this->input->post('select_gender'),
				'stu_nation' => $this->input->post('update_nation'),
				'politics_status' => $this->input->post('update_politics_status'),
				'certificate_type' => $this->input->post('update_id_type'),
				'certificate_num' => $this->input->post('update_id_num'),
				'ungra_school' => $this->input->post('update_undergraduate_school'),
				'ungra_major' => $this->input->post('update_undergraduate_major'),
				'ungra_num' => $this->input->post('update_undergraduate_id'),
				'gra_date' => $undergraduate_date->format('Y-m-d'),
				'stu_from' => $this->input->post('update_stu_from'),
				'stu_avg_score' => $this->input->post('update_avg_score'),
				'stu_cet4_score' => $this->input->post('update_level_4_score'),
				'stu_cet6_score' => $this->input->post('update_level_6_score'),
				'stu_class_rank' => $this->input->post('update_class_ranking'),
				'stu_major_rank' => $this->input->post('update_grade_ranking'),
				'stu_telephone' => $this->input->post('update_telephone'),
				'stu_cellphone' => $this->input->post('update_cellphone'),
				'apply_college' => $this->input->post('update_apply_collage'),
				'apply_major' => $this->input->post('update_apply_major'),
				'tutor_id' => $this->input->post('update_tutor'),
				'stu_address' => $this->input->post('update_address'),
				'stu_reg_reason' => $this->input->post('update_register_reason'),
				'stu_description' => $this->input->post('update_description'),
				'stu_experience' => $this->input->post('update_work_description')
			);
		if($this->input->post('select_apptype')==3)
	  {
	  	$profile_data['stu_exam_num'] = $this->input->post('update_examinee_num');
	  	$profile_data['stu_total_points'] = $this->input->post('update_total_points');
	  	$profile_data['stu_political'] = $this->input->post('update_political_points');
	  	$profile_data['stu_foreign'] = $this->input->post('update_foreign_language');
	  	$profile_data['stu_bus_one'] = $this->input->post('update_business_one');
	  	$profile_data['stu_bus_two'] = $this->input->post('update_business_two');
	  	$profile_data['stu_init_major'] = $this->input->post('update_initial_major');
	  }
			//需要判断是新增还是修改
			if($this->input->post('applyId')=='unsaved'){
				$this->db->insert('ga_apply_info',$profile_data);	
			}else{
        $this->db->where('apply_id', $this->input->post('applyId'));
        $this->db->update('ga_apply_info', $profile_data); 				
			}
							
		}	
		$this->data['message'] = validation_errors('<p class="error_msg">', '</p>');
		return FALSE;	
	}
	
	/**
	 * submit_apply_form
	 * 提交申请数据
	 */	
	function submit_apply_form(){
	  //计算流水号
	  $yearInfo = substr(date('Y-m-d',time()),2,2);
	  $applyType = $this->input->post('select_apptype');
	  $applyCollege = substr($this->input->post('update_apply_collage'),1,2);
	  
	  //获取流水号码
	  $series = $yearInfo.$applyType.$applyCollege;
	  $this->db->select('*');
	  $this->db->from('ga_apply_info');
	  $this->db->like('apply_series',$series );
	  $count = $this->db->count_all_results();
	  $count++;
    if ($count < 10){
      $series = $series.'000'.$count;
    }else if($id < 100){
    	$series = $series.'00'.$count;
    }else if($id < 1000){
      $series = $series.'0'.$count;
    }else{
    	$series = $series.$count;
    }
    //处理该流水号码，使其唯一
    while($this->db->get_where('ga_apply_info', array('apply_series' => $series))->num_rows() != 0){
      $count++;
      if ($count < 10){
        $count = '000'.$count;
      }else if($id < 100){
    	  $count = '00'.$count;
      }else if($id < 1000){
        $count = '0'.$count;
      }else{
    	  $count = $count;
      }
      $series = substr($series,0,6).$count;
    }
		$profile_data = array(
		  'apply_series' => $series,
			'apply_status' => ($this->input->post('select_apptype')=='02')?4:2
		);
    $this->db->where('apply_id', $this->input->post('applyId'));
    $this->db->update('ga_apply_info', $profile_data); 
	}	
	
	/**
	 * update_apply_form
	 * 更新“国家网络报名号”
	 */	
	function update_apply_form(){
		$profile_data = array(
		  'statue_reg_num' => $this->input->post('update_bmh')
		);
    $this->db->where('apply_id', $this->input->post('applyId'));
    $this->db->update('ga_apply_info', $profile_data); 		
	}	
}